package com.example.proyyj.Controller;


import com.example.proyyj.Entity.CollectionExhibit;
import com.example.proyyj.Entity.Exhibit;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.CollectionExhibitService;
import com.example.proyyj.Util.JSONTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/CollectionExhibit/")//请求路径
public class CollectionExhibitController {

    @Resource
    CollectionExhibitService collectionExhibitService;
    ResultEntity resultEntity=null;

    /**
     * 用户是否收藏
     * @param UserId
     * @param ExhibitId
     * @return
     */
    @GetMapping("Exhibit")
    private  String Exhibit(@RequestParam("UserId") int UserId,@RequestParam("ExhibitId") int ExhibitId){
        resultEntity = new ResultEntity();
        try{
            CollectionExhibit collection= collectionExhibitService.collectionExhibit(UserId, ExhibitId);
            if (collection !=null){

                resultEntity.setCode(0);
                resultEntity.setMsg("已收藏！");

            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未收藏！");
            }

        }catch (Exception e){

            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PostMapping("addExhibit")
    private  String addExhibit(@RequestParam("UserId") int UserId,@RequestParam("ExhibitId") int ExhibitId){
        resultEntity = new ResultEntity();
        try {
            if (collectionExhibitService.addExhibit(UserId, ExhibitId) >0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

@DeleteMapping("delExhibit")
    private String delExhibit(@RequestParam("UserId") int UserId,@RequestParam("ExhibitId") int ExhibitId){
    resultEntity = new ResultEntity();
    try {
            if (collectionExhibitService.delExhibit(UserId, ExhibitId)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("删除失败");
            }

    }catch (Exception e){
        e.printStackTrace();
        resultEntity.setCode(999);
        resultEntity.setMsg("系统错误，请联系管理员");
    }
    return JSONTool.getJsonTool().toJson(resultEntity);
}


@GetMapping("lookExhibitlist")
    private String lookExhibitlist(@RequestParam("UserId") int UserId){
    resultEntity = new ResultEntity();

    try{

        List<CollectionExhibit> list = collectionExhibitService.lookExhibitlist(UserId);
        if (list !=null){
            resultEntity.setCode(0);
            resultEntity.setMsg("查看成功");
            resultEntity.setData(list);

        }else {
            resultEntity.setCode(2);
            resultEntity.setMsg("查看失败");
        }
    }catch (Exception e){
        e.printStackTrace();
        resultEntity.setCode(999);
        resultEntity.setMsg("系统错误，请联系管理员");
    }
    return JSONTool.getJsonTool().toJson(resultEntity);
}

@GetMapping("lookExhibit")
    private  String  lookExhibit(@RequestParam("ExhibitId") int ExhibitId){
    resultEntity = new ResultEntity();
        try {
          Exhibit exhibit = collectionExhibitService.lookExhibit(ExhibitId);
            if (exhibit !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查询成功");
                resultEntity.setData(exhibit);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未查询成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
    return JSONTool.getJsonTool().toJson(resultEntity);
}

}
